/**
 * Created by python on 19-8-28.
 */

let detail = new Vue({
    el: '#app',
    data: {
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        Complain: {},
        comments: [],
        comment: "",
        username: '',
    },
    computed: {},
    mounted: function () {
        let complain_id = this.get_query_string("id", 1);
        console.log(complain_id);
        axios.get(HOST + "complains/" + complain_id + "/", {
            pramas: {}
        })
            .then(response => {

                this.Complain = response.data;
                console.log(response.data);
                this.comment = this.comment.comment;
                this.username = this.Complain.user_id.username

            }).catch(function (error) {
                console.log(error);
            }
        )
        ;
    },
    methods: {
        get_query_string: function (name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        },
    },
    // 获取评论列表
    get_comment: function () {
        alert(1)
        axios.get(host + '/spit/' + this.complain_id + '/comments/',
            {
                headers: this.headers,
                responseType: 'json'
            })
            .then(response => {
                // console.log(response.data);
                this.comments = response.data
            })
            .catch(error => {

            })
    },
    // 评论点赞
    comment_like: function (comment) {
        if (!this.token) {
            location.href = '/person-loginsign.html?next=/spit-detail.html?id=' + this.complain_id
            return
        }


        axios.post(this.host + '/spit/commentlike/',
            {
                comment: comment.id
            },
            {
                headers: this.headers,
                responseType: 'json'
            })
            .then(response => {
                // 改变该条评论里面的数据
                comment.is_like = true;
                comment.like_count += 1;
            })
            .catch(error => {

            })

    },
    // 评论取消点赞
    remove_comment_like: function (comment) {
        // 如果用户没有登录
        if (!this.token) {
            location.href = '/person-loginsign.html?next=/spit-detail.html?id=' + this.complain_id
            return
        }
        axios.delete(host + '/spit/commentlikes/' + comment.id + '/',
            {
                headers: this.headers,
                responseType: 'json'
            })
            .then(response => {

                comment.is_like = false;
                comment.like_count -= 1;
            })
            .catch(error => {

            })
    },
    //发布评论
    replay_comment: function () {
        if (!this.token) {
            location.href = '/person-loginsign.html?next=/spit-detail.html?id=' + this.complain_id
            return
        }
        if (this.new_comment_content == '') {
            alert('评论不能为空')
            return
        }

        axios.post(HOST + 'complains/' + this.Complain.id + '/comments/',
            {
                content: this.new_comment_content,
                complain_id: this.complain_id
            },
            {
                headers: this.headers,
                responseType: 'json'
            })
            .then(response => {
                // 改变该条评论里面的数据
                this.new_comment_content = ''
                this.get_comment()
                this.Complain.comment_count += 1
            })
            .catch(error => {

            })

    },
    //改变　　发布评论框　的显示状态
    change_comment_post: function () {
        this.show_comment_post = !this.show_comment_post
    },

})





